package com.shen.pool;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//Executors 相当于一个工具类
public class Demo01 {
    public static void main(String[] args) {
        /**
         * 线程的三大方法
         */
       // ExecutorService executorService = Executors.newSingleThreadExecutor();//单个线程
       // ExecutorService executorService=Executors.newFixedThreadPool(5);//固定大小的线程
        ExecutorService executorService= Executors.newCachedThreadPool();//可伸缩的，遇强则强，遇弱则弱

        try {
            for (int i = 0; i < 50; i++) {
                //使用线程池创建线程
                executorService.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" ok ");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //线程池用完，程序结束，关闭线程
            executorService.shutdown();
        }
        /**
         * ....
         * pool-1-thread-13 ok
         * pool-1-thread-14 ok
         * pool-1-thread-15 ok
         * pool-1-thread-16 ok
         * pool-1-thread-18 ok
         * pool-1-thread-19 ok
         * pool-1-thread-23 ok
         * pool-1-thread-17 ok
         * pool-1-thread-20 ok
         * pool-1-thread-21 ok
         * pool-1-thread-22 ok  可伸缩，强则强
         */


    }
}
